<?php
/**
 * Copyright (c) [2019] [吴跃忠]
 * [selibra] is licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 * http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 */

namespace Selibra\Http;


use Selibra\Http\Lib\Request;
use Selibra\Http\Lib\Response;
use Selibra\Redis\RedisClient;
use Selibra\Config\Config;
use Selibra\Tools\Console;

class Session
{


    /**
     * @var RedisClient
     */
    protected RedisClient $redis;


    /**
     * @var string
     */
    protected string $token;


    /**
     * @var Request
     */
    protected Request $request;


    /**
     * @var Response
     */
    protected Response $response;

    public function __construct($token)
    {
        $this->token = $token;
    }


    /**
     * 设置Session
     * @param $key
     * @param $value
     */
    public function set($key, $value, $expire = 24 * 3600)
    {
        $this->redisClient()->hSet($this->token, $key, $value);
        if (!empty($expire)) {
            $this->redisClient()->hSet($this->token, $key . '-expire', time() + $expire);
        }
    }


    /**
     * 获取Session
     * @param $key
     * @return mixed
     */
    public function get($key)
    {
        if (!$this->redisClient()->hExists($this->token, $key)) {
            return null;
        }
        if (!empty($this->redisClient()->hExists($this->token, $key . '-expire')) &&
            $this->redisClient()->hGet($this->token, $key . '-expire') < time()
        ) {
            $this->delete($key);
            $this->delete($key . '-expire');
            return null;
        }
        return $this->redisClient()->hGet($this->token, $key);
    }


    /**
     * @return string
     */
    public function getSessionId()
    {
        return $this->token;
    }

    /**
     * 删除Session
     * @param $key
     */
    public function delete($key)
    {
        $this->redisClient()->hDel($this->token, $key);
    }

    /**
     * @return Request
     */
    public function getRequest(): Request
    {
        return $this->request;
    }

    /**
     * @param Request $request
     */
    public function setRequest(Request $request): void
    {
        $this->request = $request;
    }

    /**
     * @return Response
     */
    public function getResponse(): Response
    {
        return $this->response;
    }

    /**
     * @param Response $response
     */
    public function setResponse(Response $response): void
    {
        $this->response = $response;
    }


    /**
     * Redis客户端连接
     * @return RedisClient
     */
    private function redisClient()
    {
        if (!isset($this->redis)) {
            $this->redis = new RedisClient();
            if (!$this->redis->hExists($this->token, 'createTime')) {
                $this->redis->hSet($this->token, 'createTime', date('Y-m-d H:i:s'));
            }
            $expireTime = Config::get('application.session.expire_time');
            if (empty($expireTime)) {
                $expireTime = 7200;
            }
            $this->redis->expire($this->token, $expireTime);
        }
        return $this->redis;
    }
}